package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

public class Pannello extends JPanel {
	private static final long serialVersionUID = -7553981536239092182L;
	private String nomeprogetto;
	public File file;
	private Pannello questo;
	private JLabel info;
	private int tipo;
	private List seq;
	List rel;
	private boolean modificato;
	public Rel oggettopremuto;
	public Rel oggettomenu;
	public Rel oggettopremutoRel;

	public Rel oggettomenuRel;
	public boolean accesso;
	public boolean ancora;

	int lx;
	int ly;
	int dimensionex;
	int dimensioney;
	private PopupRel pop;

	public int qDim = 10;
	public boolean premutoQuadratino = false;
	public int XSfondo;
	public int YSfondo;

	private ImageIcon sfondo;

	/**
	 * immagine di sfondo
	 */
	public void paint(Graphics g) {
		setOpaque(false);
		ImageIcon ii = sfondo;
		Image i = ii.getImage();
		XSfondo = ii.getIconWidth();
		YSfondo = ii.getIconHeight();
		if (XSfondo > YSfondo) {
			// Graphics2D g2d = (Graphics2D) g;
			// AffineTransform trans = new AffineTransform(0.0, 1.0, -1.0, 0.0,
			// sfondo.getIconHeight(), 0.0);
			// g2d.transform(trans);
			// g2d.drawImage(sfondo.getImage(), 0, 0, sfondo.getIconWidth(),
			// sfondo.getIconHeight(), null);
			XSfondo = ii.getIconHeight();
			YSfondo = ii.getIconWidth();
			System.out.println("sfondo panel " + XSfondo + " x " + YSfondo);
		}		
		g.drawImage(i, 0, 0, null, this);
		super.paint(g);
		repaint();

	}

	public Pannello(ImageIcon sfondo) {
		super();
		this.nomeprogetto = "";
		this.sfondo = sfondo;
		this.seq = new ArrayList();
		this.rel = new ArrayList();
		this.modificato = false;
		this.accesso = true;
		this.dimensionex = getWidth();
		this.dimensioney = getHeight();
		this.questo = this;
		setLayout(new MigLayout());
		// System.out.println("dimensione panel "+dimensionex+" x "+dimensioney);
		// if (dimensionex > dimensioney)
		// setSize(new Dimension(sfondo.getIconHeight(),
		// sfondo.getIconWidth()));
		// else
		// setSize(new Dimension(sfondo.getIconWidth(),
		// sfondo.getIconHeight()));
		setDoubleBuffered(true);
		MouseListener a = new Ascoltatore();
		MouseMotionListener b = new AscoltatoreD();
		addMouseListener(a);
		addMouseMotionListener(b);
		repaint();
		this.tipo = 1;

	}

	public void setInfo(JLabel inf) {
		this.info = inf;
	}

	public JLabel getInfo() {
		return this.info;
	}

	public int getId() {
		int num = 0;
		boolean trovato = false;

		while (!trovato) {
			num++;
			if (this.seq.contains(new Integer(num)))
				continue;
			trovato = true;
			this.seq.add(new Integer(num));
		}

		return num;
	}

	public void addId(int i) {
		this.seq.add(new Integer(i));
	}

	public String getNome() {
		return this.nomeprogetto;
	}

	public void setNome(String nome) {
		this.nomeprogetto = nome;
	}

	public Rel addRel(String nm, int direction, int type) {
		Rectangle p = getVisibleRect();
		Rel o = new Rel((int) p.getX() + 10, (int) p.getY() + 20, nm, this,
				getId(), direction, type);
		this.rel.add(o);
		repaint();
		this.modificato = true;
		return o;
	}

	public void rimuovi(Rel r) {
		this.seq.remove(new Integer(r.getId()));
		this.rel.remove(r);
		repaint();
	}

	/**
	 * disegna
	 */
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		super.paintComponent(g);
		for (int i = 0; i < this.rel.size(); i++) {
			g2.setPaint(Color.black);
			((Rel) this.rel.get(i)).disegnami(g2);

			g2.setPaint(Color.BLUE);
		}

		g2.setPaint(Color.black);

	}

	public void pulisci() {
		removeAll();
		this.rel.clear();
		this.nomeprogetto = "";
		repaint();
		this.modificato = false;
	}

	class Ascoltatore extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			Pannello.this.lx = e.getX();
			Pannello.this.ly = e.getY();

			for (int i = 0; i < Pannello.this.rel.size(); i++) {
				Rel s = (Rel) Pannello.this.rel.get(i);
				if ((!s.contains(Pannello.this.lx, Pannello.this.ly)))
					continue;
				Pannello.this.oggettopremutoRel = s;

				double xsize = Pannello.this.oggettopremutoRel.getBounds()
						.getWidth();
				double ysize = Pannello.this.oggettopremutoRel.getBounds()
						.getHeight();
				double x = Pannello.this.oggettopremutoRel.getBounds()
						.getMaxX() - xsize / 2.0D - Pannello.this.qDim / 2;
				double y = Pannello.this.oggettopremutoRel.getBounds()
						.getMaxY() - Pannello.this.qDim;

				Pannello.this.oggettomenuRel = s;
				Pannello.this.modificato = true;
			}

		}

		public void mouseReleased(MouseEvent e) {
			Pannello.this.premutoQuadratino = false;
			if (Pannello.this.oggettopremutoRel != null) {
				if ((e.getModifiers() == 4)

				) {
					Pannello.this.pop = new PopupRel(Pannello.this.questo,
							Pannello.this.oggettopremutoRel);
					Pannello.this.pop
							.show(e.getComponent(), e.getX(), e.getY());
					Pannello.this.modificato = true;
				}

				Pannello.this.ancora = false;
				Pannello.this.oggettomenuRel = null;
				Pannello.this.oggettopremutoRel = null;
			}

			Pannello.this.accesso = false;
			Pannello.this.modificato = false;

		}

		Ascoltatore() {
		}
	}

	class AscoltatoreD implements MouseMotionListener {
		public void mouseDragged(MouseEvent e) {
			e.getModifiers();

			if (e.getModifiers() == 16) {
				if (Pannello.this.oggettopremutoRel != null) {
					if (Pannello.this.premutoQuadratino) {
						Pannello.this.modificato = true;

						Pannello.this.lx = e.getX();
						Pannello.this.ly = e.getY();
					} else {
						Pannello.this.modificato = false;

//						System.out.println("y " + Pannello.this.ly + " p "
//								+ Pannello.this.YSfondo);

						
						if (Pannello.this.lx < Pannello.this.XSfondo
								&& Pannello.this.ly < Pannello.this.YSfondo
								&& Pannello.this.lx > 0 && Pannello.this.ly > 0)
							Pannello.this.oggettopremutoRel.trascina(e.getX()
									- Pannello.this.lx, e.getY()
									- Pannello.this.ly);
						Pannello.this.lx = e.getX();
						Pannello.this.ly = e.getY();
					}
				}

			}

			Pannello.this.repaint();
		}

		public void mouseMoved(MouseEvent mouseevent) {
		}

		AscoltatoreD() {
		}
	}

	public void setSfondo(ImageIcon i) {
		sfondo = i;
		setSize(new Dimension(sfondo.getIconWidth(), sfondo.getIconHeight()));
		paintAll(getGraphics());
		XSfondo = i.getIconWidth();
		YSfondo = i.getIconHeight();
	}
}